<?php

function form_message(){ 
	$view = form_header( "seleski" , "seleski"  ); 
	$title = array(
		'name'=>'title',
		'value'=>'',
		'id'=>'title',
		'type'=>'textfield',
		'size'=>'75'
	);
	$form_title= form_dynamic($title);
	$view .= form_field_display( $form_title  , "Broadcast title"  );
	
	$message = array(
		'value' => '' ,
		'name' => 'message',
			'rows'=>'10',
			'cols'=>'55',  
	);
	$form_message = form_textarea($message);
	$view .= form_field_display( $form_message   , "Message body" ,  "" );	
	
	$attachment1 = array(
		'name'=>'attachment_1',
		'value'=>'',
		'id'=>'title',
		'type'=>'file',
		'size'=>'55'
	);
	$form_att1= form_dynamic($attachment1);
	$view .= form_field_display( $form_att1  , "Lampiran 1"  );
	$attachment2 = array(
		'name'=>'attachment_2',
		'value'=>'',
		'id'=>'title',
		'type'=>'file',
		'size'=>'55'
	);
	$form_att2= form_dynamic($attachment2);
	$view .= form_field_display( $form_att2  , "Lampiran 2"  );
	
	
	$choose = array(
		'value' => '' ,
		'name' => 'choose_nik', 
			'rows'=>'2',
			'cols'=>'55',
	);
	$form_choose = form_textarea($choose);
	$view .= form_field_display( $form_choose.'<br/><font size="1">Pisahkan dengan koma</font>'   , "Khusus untuk NIK" ,  "" );	
	
	$submit = array(
		'value' => ' Send ' ,
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 

	$back = array(
		'value' => ' Batal  ' ,
		'name' => 'back', 
		'type'=>'reset','class'=>'main_button'
	);
	$form_back= form_dynamic($back); 
	$view .= form_field_display( $form_submit  .' '. $form_back   , "&nbsp;" ,  "" );	
	$view .= form_footer( );
	return $view;
}

function get_karyawan_id_by_nik($nik){
	$query = "SELECT karyawan_id FROM karyawan WHERE nik ='{$nik}' ORDER BY karyawan_id DESC LIMIT 1";
	$result = my_query($query);
	$rw = my_fetch_array($result);
	return $rw['karyawan_id'];
}
 
function send_message_to_person(){
	
	require_once('components/phpmailer/class.phpmailer.php');   
	
	$mail             = new PHPMailer();	  
	$mail->IsSMTP(); // telling the class to use SMTP 
	//$mail->SMTPDebug  = 2;                      
	//$mail->SMTPAuth   = true;   
	$mail->SMTPSecure = 'tls';              
	$mail->Host       = "localhost"; 
	$mail->Port       = 25; 
	$mail->Username = "admin"; // SMTP username 
	$mail->Password = "dordordor"; // SMTP password  
	//$mail->SetFrom('sdm.kmh@rskmd.com', 'PT. KRAKATAU MEDIKA');  
	$mail->From = 'admin@hr-kmh.com'; 
	$mail->FromName = 'SDM PT. KRAKATAU MEDIKA';
	$mail->Subject   = addslashes($_POST['title']);
	$mail->Body     = $_POST['message'] ;
 
	//SET ATTACHMENT
	if( is_file($_FILES['attachment_1']['tmp_name'] )){
		$mail->AddAttachment( $_FILES['attachment_1']['tmp_name'], $_FILES['attachment_1']['name']);
	}
	if( is_file($_FILES['attachment_2']['tmp_name'] )){
		$mail->AddAttachment( $_FILES['attachment_2']['tmp_name'], $_FILES['attachment_2']['name']);
	}
	//END SET ATTACHMENT
	
	$receivers = explode("," , trim($_POST['choose_nik']));
	$i = 0;
	if( count($receivers) > 0 and trim($_POST['choose_nik'])<> '' ){
		$mail->AddAddress("info@hr-kmh.com" , "Info SDM PTKM");
		foreach($receivers as $receiver){
			$karyawan_id = get_karyawan_id_by_nik(trim($receiver));
			if( (int)$karyawan_id > 0 ){
				$i++;
				$karyawan = loadmessage_karyawan($karyawan_id);
				//ADD ADDRESS
				$mail->AddCC($karyawan['email'], $karyawan['nama']);
			}
		}
		$mail->Send();
		return $i;
	}
	else{  
		//TO ALL
		$end=get_batas_tmt() ;
		$query = " SELECT a.karyawan_id FROM karyawan a
					INNER JOIN kekaryawanan b ON b.karyawan_id = a.karyawan_id 
					WHERE b.tmb  <= '{$end}' 
					AND a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
					AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian  WHERE tmt <= '{$end}') 	
				";	 
		$result = my_query($query);
		while($ey = my_fetch_array($result)){
			$i++; 
			$karyawan = loadmessage_karyawan($ey['karyawan_id']);
			//ADD ADDRESS
			$mail->AddAddress($karyawan['email'], $karyawan['nama']); 
		}
		$mail->Send();
		return $i;
	}
}
 

function loadmessage_karyawan($karyawan_id){
	$query = "SELECT nama ,email FROM biodata a 
		INNER JOIN karyawan b ON a.biodata_id = b.biodata_id 
		WHERE karyawan_id = {$karyawan_id} LIMIT 1";
	$result = my_query($query);
	$row = my_fetch_array($result);
	return $row;
}